﻿using Microsoft.Playwright;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore
{
    /// <summary>
    /// 淘宝任务管理器
    /// </summary>
    public class OrderTask
    {
        private static OrderTask ORDERTASK;
        private static readonly object locker = new object();
        /// <summary>
        /// 网店列表
        /// </summary>
        private List<Player> tasks = new List<Player>();
        private OrderTask(){}

        /// <summary>
        /// 一个输出服务器返回信息的方法
        /// </summary>
        public Action<string> Print { get; set; }

        /// <summary>
        /// 获取 任务管理器 实例化对象
        /// </summary>
        /// <returns>任务管理器对象</returns>
        public static OrderTask GetOrderTask()
        {
            if (ORDERTASK == null)
            {
                lock (locker)
                {
                    if(ORDERTASK == null)
                    {
                        ORDERTASK = new OrderTask();
                    }
                }
            }
            return ORDERTASK;
        }

        /// <summary>
        /// 添加指定上版账号到淘宝上版队列
        /// </summary>
        /// <param name="player"></param>
        public void Add(Player player)
        {
            tasks.Add(player);
        }

        /// <summary>
        /// 从淘宝上版队列中移除指定上版账号
        /// </summary>
        /// <param name="player"></param>
        public void Remove(Player player)
        {
            tasks.Remove(player);
        }

        /// <summary>
        /// 清空所有任务队列
        /// </summary>
        public void Clear()
        {
            tasks.Clear();
        }

        /// <summary>
        /// 获取任务队列长度
        /// </summary>
        public int TssksCount()
        {
            return tasks.Count;
        }

        /// <summary>
        /// 执行所有任务
        /// </summary>
        public async Task Run(IBrowser browser)
        {
            string content;
            int usedCount;
            if (TssksCount() < 0)
            {
                Print("*************无可执行任务，请检查车型标题是否以用完！*************");
                return;
            }

            var watch = new Stopwatch();
            foreach (Player player in tasks)
            {
                if (player != null) 
                {
                    if (player.MyStatus.Status) break;

                    content = 
                    $"网店类型：{player.Type} \n" +
                    $"网店账号：{player.User} \n" +
                    $"发布数量：{player.Count} \n" +
                    $"模板宝贝：{player.ModelBaby} \n" +
                    $"标题文件：{player.TitlePath} \n" +
                    $"TitleList：{player.TitleList.Count} \n" +
                    $"CarMap：{player.CarMap.Count} \n" +
                    $"-----------------【{player.User}】正在执行 -----------------";

                    Print(content);
                    watch.Start();
                    try
                    {
                        await player.Execute(browser);
                    }
                    catch (Exception e)
                    {
                        Print($"任务执行异常：{e.Message}");
                    }
                    watch.Stop();
                    if ((player.UsedTitles.Count - 7) < 1) 
                    {
                        usedCount = 0;
                    }else 
                    {
                        usedCount = player.UsedTitles.Count - 7;
                    }
                    content =
                    $"已用标题: {usedCount} \n" +
                    $"剩余标题: {player.CarMap.Count} \n" +
                    $"任务耗时：{watch.Elapsed} \n" +
                    $"-----------------【{player.User}】执行结束 -----------------";
                    Print(content);
                    watch.Reset();
                    Thread.Sleep(player.TimeInterval * 1000);
                }
            }
            Clear();
        }
    }
}
